package com.wugui.datax.admin.controller;

import com.baomidou.mybatisplus.extension.api.R;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.wugui.datax.admin.entity.JobDatasource;
import com.wugui.datax.admin.service.JobDatasourceService;
import com.wugui.datax.admin.tool.generate.query.GenerateQueryToolFactory;
import com.wugui.datax.admin.tool.generate.query.GenerateQueryToolInterface;
import com.wugui.datax.admin.tool.generate.util.QueryBody;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.List;

@RestController
@RequestMapping("/api/JdbcDatasource/data")
@Api(tags = "数据查询")
public class JobDatasourceDataController extends BaseController {
    /**
     * 服务对象
     */
    @Autowired
    private JobDatasourceService jobDatasourceService;

    /**
     * //通用查询条件格式（暂定）
     * {
     *   "dataBaseId": "",
     *   "tableName": "",
     *   "fields": ["",""],
     *   "conditions": {
     *       "a": 2, //where a = 2
     *       "a": null, //where g is null
     *       "a$like": "a" , //where a like '%a%'
     *       "a$gte": "1970-01-01", //where a >='1970-01-01'
     *       "a$lt": 2, //where a < 2
     *       "a$ne": 2, //where a != 2
     *       "a$ne": [1,2,3], //where a !=1 and a !=2 and a != 3
     *       "a$in": [1,2,3], //where g in'1,2,3'
     *       "$or": [ //where a > 2 or b< 2
     *         "a$gt": "1970-01-01", //where a >'1970-01-01'
     *       "a$lte": 2, //where a <= 2
     *        { "a$gt": 2 },
     *         { "b$lte": 2 }
     *       ]
     *   },
     *   "orderBy": {},
     *   "pagination": {
     *     "page": 1,
     *     "pageSize": 10
     *   }
     * }
     * @return
     * @throws IOException
     */
    @PostMapping({"/"})
    @ApiOperation("通用查询接口")
    public R<Object> getTableNamesBySchema(QueryBody queryBody) throws IOException {
        JobDatasource datasource = jobDatasourceService.getById(queryBody.getDataBaseId());


        queryBody.setDatasource(datasource);
        GenerateQueryToolInterface qTool = GenerateQueryToolFactory.getByDbType(queryBody);
        Object query = qTool.query(queryBody);
        return success(query);
    }

}